package cn.jbolt.ec.commodity;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.model.BecRetailerCommodityImg;
import cn.jbolt.ec.cloud.QnService;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;

import java.io.File;


/**
 * @author zedomi
 */
public class CommodityFileService extends JBoltBaseService<BecRetailerCommodityImg> {
    BecRetailerCommodityImg dao = new BecRetailerCommodityImg().dao();
    @Override
    protected BecRetailerCommodityImg dao() {
        return dao;
    }


    /**
     * 保存图片
     * @param key
     * @return
     */
    public Ret saveImageFile( String key) {
        BecRetailerCommodityImg becRetailerCommodityImg = new BecRetailerCommodityImg();
        becRetailerCommodityImg.setKey(key);
        boolean success = becRetailerCommodityImg.save();
        if (success)
            return fail("上传成功");
        return  fail("上传失败");
    }
    /**
     * 保存音频
     * @param userId
     * @param file
     * @param uploadPath
     * @return
     */


    /**
     * 保存文件底层方法
     * @param file
     * @param uploadPath
     * @param fileType
     * @return
     */
    public Ret saveFile(Integer userId, UploadFile file, String uploadPath, int fileType) {
        String localPath=file.getUploadPath()+"/"+file.getFileName();
        String localUrl= JFinal.me().getConstants().getBaseUploadPath()+"/"+uploadPath+"/"+file.getFileName();
        BecRetailerCommodityImg jboltFile=new BecRetailerCommodityImg();
        jboltFile.setKey(localUrl);
        File realFile=file.getFile();
        long fileSize= FileUtil.size(realFile);
        String fileSuffix= FileTypeUtil.getType(realFile);
        jboltFile.setSuffix(fileSuffix);
        boolean success=jboltFile.save();
        return success?success(localUrl,"上传成功"):fail("上传失败");
    }

}
